package com.woniu.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.account.model.UserAddress;
import com.woniu.account.dao.UserAddressDao;
import com.woniu.account.param.User.UserAddressParam;
import com.woniu.account.service.UserAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月11日
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressDao, UserAddress> implements UserAddressService {
    @Resource
    private UserAddressDao userAddressDao;
    @Override
    public void addAddress(UserAddressParam param) {
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",param.getUserId());
        UserAddress userAddress = new UserAddress();
        BeanUtil.copyProperties(param,userAddress);
        userAddressDao.insert(userAddress);
    }

    @Override
    public List<UserAddress> selectAddress(Integer userId) {
        QueryWrapper<UserAddress>wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);

        return userAddressDao.selectList(wrapper);
    }

    @Override
    public void updateAddress(UserAddressParam param) {
        QueryWrapper<UserAddress>wrapper = new QueryWrapper<>();
        wrapper.eq("address_id",param.getAddressId());
        UserAddress userAddress  = userAddressDao.selectById(param.getAddressId());
        userAddress.setAddressName(param.getAddressName());
        userAddress.setName(param.getName());
        userAddress.setPhoneNum(param.getPhoneNum());
        userAddressDao.update(userAddress,wrapper);
    }
}
